package com.ly.web.controller.system;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ly.common.constant.Constants;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.core.domain.entity.SysDept;
import com.ly.common.core.domain.entity.SysMenu;
import com.ly.common.core.domain.entity.SysUser;
import com.ly.common.core.domain.model.LoginBody;
import com.ly.common.core.domain.model.LoginUser;
import com.ly.common.utils.ServletUtils;
import com.ly.common.utils.StringUtils;
import com.ly.framework.web.service.SysLoginService;
import com.ly.framework.web.service.SysPermissionService;
import com.ly.framework.web.service.TokenService;
import com.ly.heart.domain.KingBaseCurrencyEntity;
import com.ly.heart.service.IKingBaseCurrencyService;
import com.ly.system.domain.vo.RouterVo;
import com.ly.system.service.ISysDeptService;
import com.ly.system.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.intellij.lang.annotations.RegExp;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 登录验证
 *
 * @author ly
 */
@RestController
@Slf4j
public class SysLoginController {

    @Resource
    private SysLoginService loginService;

    @Resource
    private ISysMenuService menuService;

    @Resource
    private SysPermissionService permissionService;

    @Resource
    private TokenService tokenService;

    @Resource
    private ISysDeptService deptService;

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private ISysDeptService iSysDeptService;
    @Resource
    private IKingBaseCurrencyService iKingBaseCurrencyService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping ( "/login" )
    public AjaxResult login ( @RequestBody LoginBody loginBody ) {
        AjaxResult ajax = AjaxResult.success ();
        // 生成令牌
        String token = loginService.login ( loginBody.getUsername (), loginBody.getPassword (), loginBody.getCode (),
                loginBody.getUuid () );
        ajax.put ( Constants.TOKEN, token );
        if ( Objects.nonNull ( loginBody.getDeptId () ) ) {
            redisTemplate.opsForValue ().set ( loginBody.getUsername (), loginBody.getDeptId () );
        }
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping ( "getInfo" )
    public AjaxResult getInfo () {
        LoginUser loginUser = tokenService.getLoginUser ( ServletUtils.getRequest () );
        SysUser user = loginUser.getUser ();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission ( user );
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission ( user );
        AjaxResult ajax = AjaxResult.success ();
        ajax.put ( "user", user );
        ajax.put ( "roles", roles );
        ajax.put ( "permissions", permissions );
        //获取当前操作的子公司id
        if ( Boolean.TRUE.equals ( redisTemplate.hasKey ( loginUser.getUsername () ) ) ) {
            String deptId = redisTemplate.opsForValue ().get ( loginUser.getUsername () ).toString ();
            ajax.put ( "deptId", deptId );
            SysDept sysDept = iSysDeptService.getById ( deptId );
            ajax.put ( "deptName", sysDept.getDeptName () );
            ajax.put ( "deptCode", sysDept.getDeptCode () );
            ajax.put ( "standardCurrencyCode", sysDept.getStandardCurrencyCode () );
            KingBaseCurrencyEntity one = iKingBaseCurrencyService.getOne ( new LambdaQueryWrapper<KingBaseCurrencyEntity> ().eq ( KingBaseCurrencyEntity :: getCode, sysDept.getStandardCurrencyCode () ) );
            ajax.put ( "standardCurrencyName", one.getCurrencyName () );
        }
        return ajax;
    }


    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping ( "getRouters" )
    public AjaxResult getRouters () {
        LoginUser loginUser = tokenService.getLoginUser ( ServletUtils.getRequest () );
        // 用户信息
        SysUser user = loginUser.getUser ();
        JSONObject jsonObject = menuService.selectMenuTreeByUserId ( user.getUserId () );
        List<SysMenu> childPerms = jsonObject.getJSONArray ( "childPerms" ).toJavaList ( SysMenu.class );
        List<SysMenu> noFilterChild = jsonObject.getJSONArray ( "noFilterChild" ).toJavaList ( SysMenu.class );
        List<RouterVo> voList = menuService.buildMenus ( childPerms );
        List<RouterVo> noFilterList = menuService.buildMenus ( noFilterChild );
        JSONObject object = new JSONObject ();
        object.put ( "childPerms", voList );
        object.put ( "noFilterChild", noFilterList );
        return AjaxResult.success ( object );
    }

    /**
     * 点击左侧一级目录获取子菜单
     *
     * @return
     * @author：Wuzh
     */
    @GetMapping ( "queryRouters" )
    public AjaxResult queryRouters ( String parentId ) {
        LoginUser loginUser = tokenService.getLoginUser ( ServletUtils.getRequest () );
        // 用户信息
        SysUser user = loginUser.getUser ();
        JSONObject jsonObject = menuService.queryRoutersByUserId ( user.getUserId (), parentId );
        List<SysMenu> childPerms = jsonObject.getJSONArray ( "childPerms" ).toJavaList ( SysMenu.class );
        List<SysMenu> noFilterChild = jsonObject.getJSONArray ( "noFilterChild" ).toJavaList ( SysMenu.class );
        List<RouterVo> voList = menuService.buildMenus ( childPerms );
        List<RouterVo> noFilterList = menuService.buildMenus ( noFilterChild );
        JSONObject object = new JSONObject ();
        object.put ( "childPerms", voList );
        object.put ( "noFilterChild", noFilterList );
        return AjaxResult.success ( object );
    }

    /**
     * 获取子公司下拉
     *
     * @return
     */
    @GetMapping ( "/dept/option" )
    public AjaxResult getDeptOption () {
        return AjaxResult.success ( deptService.getDeptOption () );
    }


}
